﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8" />
    <title>TrafficLight: Test/TrafficLightTest/TrafficLightTest.cs Source File</title>
    <link href="/Content/Help.css" rel="stylesheet" type="text/css" />
    <link href="/Content/css/tabs.css" rel="stylesheet" type="text/css" />
    <link href="/Content/css/doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="header">
        <img id="logo" alt="StateForge" src="/Content/Images/StateForgeLogo.png" />
        <div id="title">
            <h1>
                C# examples using state machines generated by StateBuilderDotNet
            </h1>
            <h3>
                TrafficLight: Test/TrafficLightTest/TrafficLightTest.cs Source File
            </h3>
        </div>
        <div id="menucontainer">
            <ul id="menu">
                <li><a href="/">Home</a></li>
                <li><a href="/Home/Download">Downloads</a></li>
                <li><a href="/Help/state-machine-tools.aspx">Help</a></li>
                <li><a href="/Home/Faq">F.A.Q</a></li>
                <li><a href="/Home/About">Contact us</a></li>
            </ul>
        </div>
    </div>
<!-- Generated by Doxygen 1.7.4 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
    </ul>
  </div>
<div class="header">
  <div class="headertitle">
<div class="title">Test/TrafficLightTest/TrafficLightTest.cs</div>  </div>
</div>
<div class="contents">
<a href="_traffic_light_test_8cs.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 ﻿
<a name="l00002"></a>00002 <span class="comment">//</span>
<a name="l00003"></a>00003 <span class="comment">// This class is a basic framework to test a state machine with NUnit and RhinoMock.</span>
<a name="l00004"></a>00004 <span class="comment">// Here are the steps to get started:</span>
<a name="l00005"></a>00005 <span class="comment">// * Rename MyObjectTest.cs .</span>
<a name="l00006"></a>00006 <span class="comment">// * Change the namespace.</span>
<a name="l00007"></a>00007 <span class="comment">// * Add a Reference to NUnit which can found in ExternalDlls/NUnit  </span>
<a name="l00008"></a>00008 <span class="comment">// * Add a Reference to Rhino.Mocks which can found in ExternalDlls/RhinoMocks </span>
<a name="l00009"></a>00009 <span class="comment">// * Add a Reference to your state machine. </span>
<a name="l00010"></a>00010 <span class="comment">// * Replace MyContext with your context class.</span>
<a name="l00011"></a>00011 <span class="comment">// * Replace MyActuator with your actuator class.</span>
<a name="l00012"></a>00012 
<a name="l00013"></a>00013 <span class="comment">// * Start to change the function with the [Test] attribute to implement your test</span>
<a name="l00014"></a>00014 <span class="comment">// * Compile the test project</span>
<a name="l00015"></a>00015 <span class="comment">// * Open the Nunit Gui runnner and select the test project dll</span>
<a name="l00016"></a>00016 
<a name="l00017"></a><a class="code" href="namespace_state_forge_1_1_examples_1_1_traffic_light.html">00017</a> <span class="keyword">namespace </span>StateForge.Examples.<a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light.html" title="TrafficLight is a container class which hold instances of a TrafficLightActuator and a generated Traf...">TrafficLight</a>
<a name="l00018"></a>00018 {
<a name="l00019"></a>00019     <span class="keyword">using</span> System;
<a name="l00020"></a>00020     <span class="keyword">using</span> NUnit.Framework;
<a name="l00021"></a>00021     <span class="keyword">using</span> StateForge.StateMachine;
<a name="l00022"></a>00022     <span class="keyword">using</span> Rhino.Mocks;
<a name="l00023"></a>00023     <span class="keyword">using</span> System.Threading;
<a name="l00024"></a>00024     <span class="keyword">using</span> System.Diagnostics;
<a name="l00025"></a>00025 
<a name="l00026"></a>00026     [TestFixture]
<a name="l00027"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html">00027</a>     <span class="keyword">class </span><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html">TrafficLightTest</a>
<a name="l00028"></a>00028     {
<a name="l00029"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html#adac2af3232210826c10d4ef27e6b9fd4">00029</a>         <span class="keyword">private</span> TrafficLightContext <a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html#adac2af3232210826c10d4ef27e6b9fd4">context</a>;  
<a name="l00030"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html#a0935edcb49fa5bd9eb880a3211efb9b9">00030</a>         <span class="keyword">private</span> <a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_light.html" title="The Light class knows how to switch on and off the green, yellow and red lights.">Light</a> <a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html#a0935edcb49fa5bd9eb880a3211efb9b9">light</a>;
<a name="l00031"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html#a0b2d55fba0d0047eed1316a4019ee690">00031</a>         <span class="keyword">private</span> MockRepository <a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html#a0b2d55fba0d0047eed1316a4019ee690">repository</a>;
<a name="l00032"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html#acbe28b7afea03021e875806af9b80085">00032</a>         <span class="keyword">private</span> IObserver <a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html#acbe28b7afea03021e875806af9b80085">observer</a>;
<a name="l00033"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html#a1055ce26d36a81218a1ebd90bf7840bd">00033</a>         <span class="keyword">private</span> <span class="keyword">static</span> String contextName = <span class="stringliteral">&quot;TrafficLightContext&quot;</span>;
<a name="l00034"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html#af0ba597ea0f15da66cf4c2c8d8d3186a">00034</a>         <span class="keyword">private</span> <span class="keyword">static</span> TraceSource ts = <span class="keyword">new</span> TraceSource(<span class="stringliteral">&quot;TrafficLight&quot;</span>);
<a name="l00035"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html#ac798b89b9ec2cf8a26352729d3421d40">00035</a>         <span class="keyword">private</span> <span class="keyword">static</span> AutoResetEvent autoResetEvent = <span class="keyword">new</span> AutoResetEvent(<span class="keyword">false</span>);
<a name="l00036"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html#a376cda73bb5544077f0a7439ba3281d7">00036</a>         <span class="keyword">private</span> <span class="keywordtype">string</span> current, next, <a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html#a376cda73bb5544077f0a7439ba3281d7">transition</a>;
<a name="l00037"></a>00037 
<a name="l00038"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html#a99920d1497921ff22547726dd15eca0e">00038</a>         <span class="keyword">public</span> <a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html">TrafficLightTest</a>()
<a name="l00039"></a>00039         {
<a name="l00040"></a>00040             Setup();
<a name="l00041"></a>00041         }
<a name="l00042"></a>00042 
<a name="l00043"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html#a23a761d719302a23ad85adba0ee807a8">00043</a>         <span class="keyword">private</span> <span class="keywordtype">void</span> StateMachineEnd(<span class="keywordtype">object</span> sender, EventArgs e)
<a name="l00044"></a>00044         {
<a name="l00045"></a>00045             Console.WriteLine(<span class="stringliteral">&quot;State machine has ended&quot;</span>);
<a name="l00046"></a>00046             autoResetEvent.Set();
<a name="l00047"></a>00047         }
<a name="l00048"></a>00048 
<a name="l00049"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html#ab1f448cda1f6b272bebbbdd97390e299">00049</a>         <span class="keyword">static</span> <span class="keywordtype">void</span> Main(<span class="keywordtype">string</span>[] args)
<a name="l00050"></a>00050         {
<a name="l00051"></a>00051             <a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html">TrafficLightTest</a> trafficLightTest = <span class="keyword">new</span> <a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html">TrafficLightTest</a>();
<a name="l00052"></a>00052             trafficLightTest.<a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html#a205166958da902ea876f315b5bd694fc">NormalOperation</a>();
<a name="l00053"></a>00053             autoResetEvent.WaitOne(2000);
<a name="l00054"></a>00054             ts.TraceInformation(<span class="stringliteral">&quot;Test has ended&quot;</span>);
<a name="l00055"></a>00055             Environment.Exit(0);
<a name="l00056"></a>00056         }
<a name="l00057"></a>00057 
<a name="l00058"></a>00058         [SetUp]
<a name="l00059"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html#af75e0a034539ff6081e523db72a3b996">00059</a>         <span class="keyword">public</span> <span class="keywordtype">void</span> Setup()
<a name="l00060"></a>00060         {
<a name="l00061"></a>00061             repository = <span class="keyword">new</span> MockRepository();
<a name="l00062"></a>00062             this.light = <span class="keyword">new</span> <a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_light.html" title="The Light class knows how to switch on and off the green, yellow and red lights.">Light</a>();
<a name="l00063"></a>00063             this.observer = repository.StrictMock&lt;IObserver&gt;();
<a name="l00064"></a>00064             this.context = <span class="keyword">new</span> TrafficLightContext(this.light);
<a name="l00065"></a>00065             this.context.Observer = this.observer;
<a name="l00066"></a>00066             this.context.EndHandler += <span class="keyword">new</span> EventHandler&lt;EventArgs&gt;(StateMachineEnd);
<a name="l00067"></a>00067         }
<a name="l00068"></a>00068 
<a name="l00069"></a>00069         [TearDown]
<a name="l00070"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html#a9e344f3baf873beaf9550599fb614690">00070</a>         <span class="keyword">public</span> <span class="keywordtype">void</span> TearDown()
<a name="l00071"></a>00071         {
<a name="l00072"></a>00072 
<a name="l00073"></a>00073         }
<a name="l00074"></a>00074 
<a name="l00075"></a>00075         [Test]
<a name="l00076"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light_test.html#a205166958da902ea876f315b5bd694fc">00076</a>         <span class="keyword">public</span> <span class="keywordtype">void</span> NormalOperation()
<a name="l00077"></a>00077         {
<a name="l00078"></a>00078             <span class="keyword">using</span> (repository.Record())
<a name="l00079"></a>00079             {
<a name="l00080"></a>00080                 <span class="keyword">using</span> (repository.Ordered())
<a name="l00081"></a>00081                 {
<a name="l00082"></a>00082                     <span class="comment">// this.context.EnterInitialState();</span>
<a name="l00083"></a>00083                     Expect.Call(() =&gt; this.observer.OnEntry(contextName, <span class="stringliteral">&quot;Root&quot;</span>));
<a name="l00084"></a>00084                     Expect.Call(() =&gt; this.observer.OnTimerStart(contextName, <span class="stringliteral">&quot;MaxDuration&quot;</span>, light.TimerMaxDuration));
<a name="l00085"></a>00085                     Expect.Call(() =&gt; this.observer.OnEntry(contextName, <span class="stringliteral">&quot;Red&quot;</span>));
<a name="l00086"></a>00086                     Expect.Call(() =&gt; this.observer.OnTimerStart(contextName, <span class="stringliteral">&quot;TimerRed&quot;</span>, light.TimerRedDuration));
<a name="l00087"></a>00087                     <span class="comment">// this.context.EvTimerRed();</span>
<a name="l00088"></a>00088                     current = <span class="stringliteral">&quot;Red&quot;</span>; next = <span class="stringliteral">&quot;Green&quot;</span>; transition = <span class="stringliteral">&quot;EvTimerRed&quot;</span>;
<a name="l00089"></a>00089                     Expect.Call(() =&gt; this.observer.OnTransitionBegin(contextName, current, next, transition));
<a name="l00090"></a>00090                     Expect.Call(() =&gt; this.observer.OnExit(contextName, current));
<a name="l00091"></a>00091                     Expect.Call(() =&gt; this.observer.OnTimerStop(contextName, <span class="stringliteral">&quot;TimerRed&quot;</span>));
<a name="l00092"></a>00092                     Expect.Call(() =&gt; this.observer.OnEntry(contextName, next));
<a name="l00093"></a>00093                     Expect.Call(() =&gt; this.observer.OnTimerStart(contextName, <span class="stringliteral">&quot;TimerGreen&quot;</span>, light.TimerGreenDuration));
<a name="l00094"></a>00094                     Expect.Call(() =&gt; this.observer.OnTransitionEnd(contextName, current, next, transition));
<a name="l00095"></a>00095 
<a name="l00096"></a>00096                     <span class="comment">// this.context.EvTimerGreen();</span>
<a name="l00097"></a>00097                     current = <span class="stringliteral">&quot;Green&quot;</span>; next = <span class="stringliteral">&quot;Yellow&quot;</span>; transition = <span class="stringliteral">&quot;EvTimerGreen&quot;</span>;
<a name="l00098"></a>00098                     Expect.Call(() =&gt; this.observer.OnTransitionBegin(contextName, current, next, transition));
<a name="l00099"></a>00099                     Expect.Call(() =&gt; this.observer.OnExit(contextName, current));
<a name="l00100"></a>00100                     Expect.Call(() =&gt; this.observer.OnEntry(contextName, next));
<a name="l00101"></a>00101                     Expect.Call(() =&gt; this.observer.OnTimerStart(contextName, <span class="stringliteral">&quot;TimerYellow&quot;</span>, light.TimerYellowDuration));
<a name="l00102"></a>00102                     Expect.Call(() =&gt; this.observer.OnTransitionEnd(contextName, current, next, transition));
<a name="l00103"></a>00103 
<a name="l00104"></a>00104                     <span class="comment">// this.context.EvTimerYellow();</span>
<a name="l00105"></a>00105                     current = <span class="stringliteral">&quot;Yellow&quot;</span>; next = <span class="stringliteral">&quot;Red&quot;</span>; transition = <span class="stringliteral">&quot;EvTimerYellow&quot;</span>;
<a name="l00106"></a>00106                     Expect.Call(() =&gt; this.observer.OnTransitionBegin(contextName, current, next, transition));
<a name="l00107"></a>00107                     Expect.Call(() =&gt; this.observer.OnExit(contextName, current));
<a name="l00108"></a>00108                     Expect.Call(() =&gt; this.observer.OnEntry(contextName, next));
<a name="l00109"></a>00109                     Expect.Call(() =&gt; this.observer.OnTimerStart(contextName, <span class="stringliteral">&quot;TimerRed&quot;</span>, light.TimerRedDuration));
<a name="l00110"></a>00110                     Expect.Call(() =&gt; this.observer.OnTransitionEnd(contextName, current, next, transition));
<a name="l00111"></a>00111 
<a name="l00112"></a>00112                     <span class="comment">// this.context.EvTimerMaxDuration();</span>
<a name="l00113"></a>00113                     current = <span class="stringliteral">&quot;Red&quot;</span>; next = <span class="stringliteral">&quot;End&quot;</span>; transition = <span class="stringliteral">&quot;EvTimerMaxDuration&quot;</span>;
<a name="l00114"></a>00114                     Expect.Call(() =&gt; this.observer.OnTransitionBegin(contextName, current, next, transition));
<a name="l00115"></a>00115                     Expect.Call(() =&gt; this.observer.OnExit(contextName, current));
<a name="l00116"></a>00116                     Expect.Call(() =&gt; this.observer.OnTimerStop(contextName, <span class="stringliteral">&quot;TimerRed&quot;</span>));
<a name="l00117"></a>00117                     Expect.Call(() =&gt; this.observer.OnEntry(contextName, next));
<a name="l00118"></a>00118                     Expect.Call(() =&gt; this.observer.OnTransitionEnd(contextName, current, next, transition));
<a name="l00119"></a>00119                 }
<a name="l00120"></a>00120             }
<a name="l00121"></a>00121 
<a name="l00122"></a>00122             <span class="keyword">using</span> (repository.Playback())
<a name="l00123"></a>00123             {
<a name="l00124"></a>00124                 Console.WriteLine(<span class="stringliteral">&quot;Entering state machine&quot;</span>);
<a name="l00125"></a>00125                 this.context.EnterInitialState();
<a name="l00126"></a>00126                 Console.WriteLine(<span class="stringliteral">&quot;Wait for end of state machine&quot;</span>);
<a name="l00127"></a>00127                 autoResetEvent.WaitOne(2000);
<a name="l00128"></a>00128             }
<a name="l00129"></a>00129         }
<a name="l00130"></a>00130 
<a name="l00131"></a>00131         
<a name="l00132"></a>00132     }
<a name="l00133"></a>00133 }
</pre></div></div>
</div>
﻿<div id="footer">
    <hr />
    <p>
        Copyright &copy; 2011 StateForge.</p>
    <hr />
</div>
</body> </html> 
